package com.ok.sb.controller;

import com.ok.sb.service.UserService;
import com.ok.sb.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author ok
 * @since 2021-08-16 22:48:15
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * hello
     *
     * @param name 名称
     * @return JSP
     */
    @ApiOperation(value = "hello")
    @ApiImplicitParam(dataType = "String", name = "name", value = "名称", required = true)
    @GetMapping("/hello")
    public String hello(String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParam(dataType = "int", name = "id", value = "用户ID", required = true)
    @GetMapping("/findById/{id}")
    @ResponseBody
    public ResultVO findById(@PathVariable("id") Integer id) {
        return ResultVO.success(userService.findById(id));
    }

    /**
     * 通过主键查询单条数据-DAO方法上写SQL
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过ID-SQL查询")
    @ApiImplicitParam(dataType = "int", name = "id", value = "用户ID", required = true)
    @GetMapping("/findByIdSql")
    @ResponseBody
    public ResultVO findByIdSql(Integer id) {
        return ResultVO.success(userService.findByIdSql(id));
    }

    /**
     * 通过主键查询单条数据 XML文件写SQL
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过ID-XML查询")
    @ApiImplicitParam(dataType = "int", name = "id", value = "用户ID", required = true)
    @GetMapping("/findByIdXml")
    @ResponseBody
    public ResultVO findByIdXml(Integer id) {
        return ResultVO.success(userService.findByIdXml(id));
    }

    /**
     * 保存
     *
     * @param name     名称
     * @param password 密码
     * @return 单条数据
     */
    @ApiOperation(value = "保存")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "name", value = "账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "密码", required = true)})
    @GetMapping("/save")
    @ResponseBody
    public ResultVO save(String name, String password) {
        return ResultVO.success(userService.save(name, password));
    }
}